package test.self.annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


public class MainClass {
	private String s1;
	private String s2;
	
	public void paseAnnotation(Object obj) {
		Field[] fields = obj.getClass().getDeclaredFields();
		Method[] methods = obj.getClass().getDeclaredMethods();
		for(Method method:methods) {
			Annotation[] ans = method.getDeclaredAnnotations();
			for(Annotation an:ans){
				System.out.println("antotaion: "+an);
			}
		}
	}
	
	public void paseAnnotation2(Object obj) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException {
		Field[] fields = obj.getClass().getDeclaredFields();
		Method[] methods = obj.getClass().getDeclaredMethods();
		for (Method method : methods) {
			String value = "";
			Test testAnnotation = method.getAnnotation(Test.class);
			if (testAnnotation != null) {
				value = (String)method.invoke(obj, null);
				System.out.println("Test annotation id= "+testAnnotation.id());
				System.out.println("Test annotation des= "+testAnnotation.description());
				System.out.println("Test annotation FullType= "+testAnnotation.foolType());
				
				if(testAnnotation.foolType() == FullType.ALL) {
					value = "%"+value+"%";
				}
				else if(testAnnotation.foolType() == FullType.LEFT)
				{
					value = "%"+value;
				}else if(testAnnotation.foolType() == FullType.RIGHT) {
					value = value+"%";
				} else {
					value = value;
				}
				Method m_ethod = obj.getClass().getMethod("setS1", String.class);
				m_ethod.invoke(obj, value);
			}
		}
	}
	
	public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException {
		MainClass m= new MainClass();
		m.setS1("abc");
		m.paseAnnotation(m);
		System.out.println("===========");
		m.paseAnnotation2(m);
		System.out.println(m.getS1());
	}

	@Test(id=1,foolType = FullType.LEFT)
	public String getS1() {
		return s1;
	}

	/**
	 * @param s1 the s1 to set
	 */
	public void setS1(String s1) {
		this.s1 = s1;
	}

	/**
	 * @return the s2
	 */
	public String getS2() {
		return s2;
	}

	/**
	 * @param s2 the s2 to set
	 */
	public void setS2(String s2) {
		this.s2 = s2;
	}
}
